package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjNetworkServiceArea;
import com.zb.mapper.ZjNetworkServiceAreaMapper;
import com.zb.service.ZjNetworkServiceAreaService;
import com.zb.vo.NetworkServiceAreaVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 网点服务区域表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-18
 */
@Service
public class ZjNetworkServiceAreaServiceImpl extends ServiceImpl<ZjNetworkServiceAreaMapper, ZjNetworkServiceArea> implements ZjNetworkServiceAreaService {

    // 保存、修改、删除服务区域
    @Override
    @Transactional
    public void save(Long custAccId, Long networkId, List<NetworkServiceAreaVo> serviceAreas) {
        // 先根据服务商ID清空旧的数据
        QueryWrapper<ZjNetworkServiceArea> wrapper = new QueryWrapper<>();
        wrapper.eq("network_id", networkId);
        this.remove(wrapper);
        if (CollectionUtils.isEmpty(serviceAreas)) {
            return;
        }
        List<ZjNetworkServiceArea> adds = new ArrayList<>();
        ZjNetworkServiceArea area = null;
        for (NetworkServiceAreaVo vo : serviceAreas) {
            area = new ZjNetworkServiceArea();
            BeanUtil.copyProperties(vo, area);
            area.setId(IdWorker.getId());
            area.setCustAccId(custAccId);
            area.setNetworkId(networkId);
            adds.add(area);
        }
        this.saveBatch(adds);
    }

    // 根据服务商ID获取服务区域
    @Override
    public List<ZjNetworkServiceArea> getByNetworkId(Long networkId) {
        QueryWrapper<ZjNetworkServiceArea> wrapper = new QueryWrapper<>();
        wrapper.select("id", "province", "city", "area", "town");
        wrapper.eq("network_id", networkId);
        return this.list(wrapper);
    }

    // 根据服务商ID批量删除服务区域
    @Override
    public void deleteBatchByIds(List<Long> networkIds) {
        if (CollectionUtils.isEmpty(networkIds)) {
            return;
        }
        QueryWrapper<ZjNetworkServiceArea> wrapper = new QueryWrapper<>();
        wrapper.in("network_id", networkIds);
        this.remove(wrapper);
    }

    // 根据服务商ID获取服务区域
    @Override
    public List<ZjNetworkServiceArea> getByNetworkIds(List<Long> networkIds) {
        if (CollectionUtil.isEmpty(networkIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkServiceArea> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_id", "province", "city", "area", "town")
                .in("network_id", networkIds);
        return this.list(wrapper);
    }

    // 获取服务商的服务区域
    @Override
    public List<NetworkServiceAreaVo> getNetworkArea(Long networkId) {
        List<ZjNetworkServiceArea> areaList = getByNetworkId(networkId);
        if (CollectionUtil.isEmpty(areaList)) {
            return new ArrayList<>();
        }
        return areaList.stream().map(area -> {
            NetworkServiceAreaVo vo = new NetworkServiceAreaVo();
            BeanUtil.copyProperties(area, vo);
            return vo;
        }).toList();
    }
}
